import express from "express"
import baseRouter from "./router/baseRouter";
import error404 from "./middleware/error404";
import { isPortOccupied } from "../utils/utils";

export default class Server {
  app = new express();
  port: number;

  constructor(port = 3000, router) {
    this.app.use(baseRouter);
    if (router) this.app.use(router);
    this.app.use(error404);

    if (port) this.port = port;

    isPortOccupied(port, (occupied) => {
      if (!occupied) {    
        this.app.listen(port);
      }
    });
  }
}
